# coding: utf-8
import sys

from PySide6.QtCore import QSize
from PySide6.QtWidgets import QFrame, QApplication, QWidget, QHBoxLayout

from qfluentwidgets import HorizontalFlipView, BodyLabel, RoundMenu, Action, MenuAnimationType
from qfluentwidgets import FluentIcon as FIF


IMAGE_TEXT_PAIRS = [
    (r"G:\TEST\TEST_\dst\113-6722631-5461860_77939273310801\113-6722631-5461860_77939273310801.jpg", 
     r"G:\TEST\TEST_\dst\113-6722631-5461860_77939273310801\113-6722631-5461860_77939273310801.txt"),
    (r"G:\TEST\TEST_\dst\113-6722631-5461860_77939273310841\113-6722631-5461860_77939273310841.jpg", 
     r"G:\TEST\TEST_\dst\113-6722631-5461860_77939273310841\113-6722631-5461860_77939273310841.txt"),
    (r"G:\TEST\TEST_\dst\113-6722631-5461860_77939273310881\113-6722631-5461860_77939273310881.jpg", 
     r"G:\TEST\TEST_\dst\113-6722631-5461860_77939273310881\113-6722631-5461860_77939273310881.txt")
]


class CopyableLabel(BodyLabel):

    def __init__(self, parent=None):
        super().__init__(parent)

    def contextMenuEvent(self, e):
        menu = RoundMenu(self)
        copy_action = Action(FIF.COPY, "复制")
        copy_action.triggered.connect(self.copy_text)
        menu.addAction(copy_action)

        # show menu
        menu.exec(e.globalPos(), aniType=MenuAnimationType.DROP_DOWN)

    def copy_text(self):
        clipboard = QApplication.clipboard()
        clipboard.setText(self.text())


class InformationView(QFrame):

    def __init__(self, parent=None, image_text_pairs:list=None):
        super().__init__(parent)

        layout = QHBoxLayout(self)
        self.image_text_pairs = image_text_pairs
        
        self.flip_view = HorizontalFlipView(self)
        self.flip_view.setFixedSize(QSize(400, 400))
        self.flip_view.setItemSize(QSize(400, 400))
        self.flip_view.setSpacing(0)
        # self.flip_view.setContentsMargins(0, 0, 0, 0)
        # self.flip_view.setFrameShape(QFrame.NoFrame)
        self.flip_view.currentIndexChanged.connect(self.update_text_label)
        
        self.setLayout(layout)
        self.style_sheet()

        self.text_label = CopyableLabel(self)
        self.text_label.setStyleSheet("padding-left: 20px;")
        self.text_label.setFixedSize(QSize(360, 400))

        layout.addWidget(self.flip_view)
        layout.addWidget(self.text_label)
        
        if image_text_pairs:
            for image_path, text_path in image_text_pairs:
                self.flip_view.addImage(image_path)
            
            self.update_text_label(0)  # 初始化时加载第一个文本内容

    def style_sheet(self):
        self.setStyleSheet("border: 1px solid rgba(0, 0, 0, 0.15);")
    
    def update_text_label(self, index):
        text_path = self.image_text_pairs[index][1]
        try:
            with open(text_path, 'r', encoding='utf-8') as file:
                text = file.read()
                self.text_label.setText(text)
        except Exception as e:
            self.text_label.setText(f"Error: {e}")

    def load_data(self, image_text_pairs):
        self.image_text_pairs = image_text_pairs
        if not image_text_pairs:
            return
        for image_path, text_path in image_text_pairs:
            image_path = str(image_path)
            self.flip_view.addImage(image_path)
        
        self.update_text_label(0)  # 初始化时加载第一个文本内容

    def _init_info(self):
        self.flip_view.clear()
        self.text_label.setText("")


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("QFrame Container Example")

        # 创建 InformationView 并添加到主布局中
        self.info_view = InformationView(self, IMAGE_TEXT_PAIRS)

        # 创建主布局
        main_layout = QHBoxLayout(self)
        main_layout.addWidget(self.info_view)

        self.setLayout(main_layout)

if __name__ == '__main__':
    app = QApplication(sys.argv)

    window = MainWindow()
    window.resize(800, 400)  # 调整窗口大小
    window.show()

    sys.exit(app.exec())